package uiLayer;

import uiLayer.ScheduleTappedPane;
import uiLayer.TeachingBlockGUI;
import controlLayer.ConflictCtrl;
import controlLayer.ScheduleCtrl;
import modelLayer.Class;

import java.util.List;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame {
	
	private ScheduleTappedPane scheduleTappedPane;
	private ScheduleCtrl scheduleCtrl = new ScheduleCtrl();
	private	ConflictCtrl conflictCtrl = new ConflictCtrl();
	private List<Class> classes = new ArrayList<>();
	private JPanel contentPane;
	private JPanel mainPanel = new JPanel();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public MainFrame () {
		mainFrameGui();
		mainPanel.add(new StaffGUI(), BorderLayout.CENTER);
	}
	
	public void mainFrameGui () {
		
		setTitle("Scheduling Management");
	    setSize(750, 500);
	    setLocationRelativeTo(null);
	    setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		JButton staffBtn = new JButton ("Staff");
		staffBtn.setPreferredSize(new Dimension(125, 40));
		staffBtn.setMinimumSize(new Dimension(125, 40));
		staffBtn.setMaximumSize(new Dimension(125, 40));
		
		staffBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				mainPanel.removeAll();
				mainPanel.add(new StaffGUI(), BorderLayout.CENTER);
				mainPanel.updateUI();
			}
		});
		
		JButton subjectBtn = new JButton ("Subjects");
		subjectBtn.setPreferredSize(new Dimension(125, 40));
		subjectBtn.setMinimumSize(new Dimension(125, 40));
		subjectBtn.setMaximumSize(new Dimension(125, 40));
		
		subjectBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				mainPanel.removeAll();
				mainPanel.add(new SubjectGUI(), BorderLayout.CENTER);
				mainPanel.updateUI();
			}
		});
		
		JButton classBtn = new JButton ("Classes");
		classBtn.setPreferredSize(new Dimension(125, 40));
		classBtn.setMinimumSize(new Dimension(125, 40));
		classBtn.setMaximumSize(new Dimension(125, 40));
		
		classBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				mainPanel.removeAll();
				mainPanel.add(new ClassGUI(), BorderLayout.CENTER);
				mainPanel.updateUI();
			}
		});
		
		JButton roomBtn = new JButton ("Rooms");
		roomBtn.setPreferredSize(new Dimension(125, 40));
		roomBtn.setMinimumSize(new Dimension(125, 40));
		roomBtn.setMaximumSize(new Dimension(125, 40));
		
		roomBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				mainPanel.removeAll();
				mainPanel.add(new RoomGUI(), BorderLayout.CENTER);
				mainPanel.updateUI();
			}
		});
		
		JButton teachingBlockBtn = new JButton ("Teaching Blocks");
		teachingBlockBtn.setPreferredSize(new Dimension(125, 40));
		teachingBlockBtn.setMinimumSize(new Dimension(125, 40));
		teachingBlockBtn.setMaximumSize(new Dimension(125, 40));
		
		teachingBlockBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed (ActionEvent event) {
				
				mainPanel.removeAll();
				
				mainPanel.add(new TeachingBlockGUI(), BorderLayout.CENTER);
				
				mainPanel.updateUI();
			}
		});
		
		JButton scheduleBtn = new JButton ("Schedule");
		scheduleBtn.setPreferredSize(new Dimension(125, 40));
		scheduleBtn.setMinimumSize(new Dimension(125, 40));
		scheduleBtn.setMaximumSize(new Dimension(125, 40));
		
		scheduleBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				mainPanel.removeAll();
				
				scheduleTappedPane = new ScheduleTappedPane();
				
				classes = conflictCtrl.getClasses();
				
				scheduleCtrl.createBlankSlade(classes);
				
				for (int i = 0; i < classes.size(); ++i) {
					
					String tabTitle = classes.get(i).getName();			
					scheduleTappedPane.add(tabTitle, new ClassSchedule(classes.get(i)));
					
				}
				
				mainPanel.add(scheduleTappedPane, BorderLayout.CENTER);
				
				mainPanel.updateUI();
			}
		});
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		contentPane.add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new BorderLayout(0, 0));
		
		JToolBar buttons = new JToolBar(JToolBar.VERTICAL);
		buttons.setFloatable(false);
		buttons.setMargin(new Insets(0, 0, 0, 5));
		
		buttons.add(staffBtn);
		buttons.add(subjectBtn);
		buttons.add(classBtn);
		buttons.add(roomBtn);
		buttons.add(teachingBlockBtn);
		buttons.add(scheduleBtn);
		
		add(buttons, BorderLayout.WEST);
	}

}